package com.xh.one2one.lesson.service.impl;

import com.xh.bussiness.resource.model.Material;
import com.xh.bussiness.resource.service.IMaterialService;
import com.xh.one2one.lesson.bean.dto.LessonTimesDto;
import com.xh.one2one.lesson.bean.request.LessonTimesHisRequest;
import com.xh.one2one.lesson.bean.request.LessonTimesRequest;
import com.xh.one2one.lesson.model.LessonTimesHis;
import com.xh.one2one.lesson.dao.LessonTimesHisMapper;
import com.xh.one2one.lesson.service.ILessonTimesHisService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.one2one.lesson.service.ILessonTimesService;
import com.xh.one2one.teachHandout.bean.dto.HandoutDto;
import com.xh.one2one.teachHandout.bean.dto.HandoutKnowledgeDto;
import com.xh.one2one.teachHandout.bean.request.TeachTimesHandoutRelationRequest;
import com.xh.one2one.teachHandout.model.TeachTimesHandoutRelation;
import com.xh.one2one.teachHandout.service.ITeachTimesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 备课 教学计划的历史版本 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-04-22
 */
@Service
public class LessonTimesHisServiceImpl extends BaseServiceImpl<LessonTimesHisMapper, LessonTimesHis> implements ILessonTimesHisService {

    @Autowired
    private ILessonTimesService lessonTimesService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private ITeachTimesService teachTimesService;

    @Override
    public XhPage queryLessonTimesHisPage(XhPage page, LessonTimesHisRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonTimesHis>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryLessonTimesHis(LessonTimesHisRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonTimesHis>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public Map<String, List> queryLessonTimesContrast(LessonTimesHisRequest bean) {
        Map retMap = new HashMap<String,List>();
        String lessonId = bean.getLessonId();

        LessonTimesRequest lessonTimesRequest = new LessonTimesRequest();
        lessonTimesRequest.setLessonId(lessonId);
        List<LessonTimesDto> lessonTimes = lessonTimesService.queryAllLessonTimes(lessonTimesRequest);
        retMap.put("current",lessonTimes);
//        if(CollectionUtils.isEmpty(lessonTimes)){
//            return  retMap;
//        }

        List<LessonTimesDto> hisList = queryHisContent(bean);
        retMap.put("old",hisList);
        int size = hisList.size();
        boolean empty = size == 0;
        for (int i = 0; i < lessonTimes.size(); i++) {
            LessonTimesDto lessonTimesDto = lessonTimes.get(i);
            if(empty){
                lessonTimesDto.setNewly(1);
                continue;
            }
            if(i >= size){
                lessonTimesDto.setNewly(1);
                continue;
            }
            LessonTimesDto lessonTimesHis = hisList.get(i);

            String newName = lessonTimesDto.getIndexName();
            String oldName = lessonTimesHis.getIndexName();
            if(!oldName.equals(newName)){
                lessonTimesDto.setNewly(1);
                continue;
            }

            HandoutDto child = lessonTimesDto.getChild();
            HandoutDto hisChild = lessonTimesHis.getChild();
            String childName = child.getName();
            String hisChildName = hisChild.getName();
            if(null != childName ){
                if(!childName.equals(hisChildName)){
                    lessonTimesDto.setNewly(1);
                    continue;
                }
            }

            List<HandoutKnowledgeDto> newKnowledges = child.getChilds();
            List<HandoutKnowledgeDto> oldKnowledges = hisChild.getChilds();
            if(!CollectionUtils.isEmpty(newKnowledges)){
                for (int j = 0; j < newKnowledges.size(); j++) {
                    HandoutKnowledgeDto newDto = newKnowledges.get(j);
                    HandoutKnowledgeDto oldDto = oldKnowledges.get(j);
                    if(CollectionUtils.isEmpty(oldDto.getKnowledgeNames()) && !CollectionUtils.isEmpty(newDto.getKnowledgeNames())){
                        lessonTimesDto.setNewly(1);
                        continue;

                    }
                    if(!CollectionUtils.isEmpty(newDto.getKnowledgeNames())){
                        List<String>  noContains= newDto.getKnowledgeNames().stream().filter(knowledgeName -> !newDto.getKnowledgeNames().contains(knowledgeName)).collect(Collectors.toList());
                        if(!CollectionUtils.isEmpty(noContains)){
                            lessonTimesDto.setNewly(1);
                            continue;
                        }
                    }
                }
            }
        }

        return retMap;
    }

    public List<LessonTimesDto> queryHisContent(LessonTimesHisRequest bean) {
        List<LessonTimesDto> lessonTimesDtos = new ArrayList<>();

        QueryWrapper qw=new QueryWrapper<LessonTimesHis>();
        qw.eq("lesson_id",bean.getLessonId());
        qw.eq("version",bean.getVersion());
        qw.orderByAsc("sort_num");
        List<LessonTimesHis> list = this.dao.selectList(qw);
        if(!CollectionUtils.isEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                LessonTimesDto lessonTimesDto = new LessonTimesDto();
                LessonTimesHis lessonTimesHis = list.get(i);
                BeanUtils.copyProperties(lessonTimesHis,lessonTimesDto);
                Integer type = lessonTimesHis.getType();
                String handoutId = lessonTimesHis.getHandoutId();
                HandoutDto handoutDto = new HandoutDto();
                handoutDto.setHandoutId(handoutId);
                handoutDto.setType(type);
                if(1 == type){
                    //type:1 查询上传的讲义，获取path
                    Material material = materialService.getById(handoutId);
                    handoutDto.setName(material.getFileName());
                    handoutDto.setPath(material.getFilePath());

                }else{
                    //type:2,3  查询自建讲义，获取讲义的知识元，知识点内容
                    handoutDto.setChilds(teachTimesService.queryHandoutContent(handoutId));
                }
                lessonTimesDto.setChild(handoutDto);
                lessonTimesDtos.add(lessonTimesDto);
            }

        }
        return lessonTimesDtos;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(LessonTimesHisRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
